<?php
//require_once '../defs/constants.php';
require_once FILE_DB_DATABASE;

class DbRisicoaandachtsgebied extends Database{

    private $document_id = '';
    private $risicoaandachtsgebied_id = '';
    private $risicoaandachtsgebied = '';
    private $array_actie_id = array();
    
    public function DbRisicoaandachtsgebied($id= ''){
        //if (DEBUG_LVL & DEBUG_LVL_CLASS) echo __FILE__ . __LINE__ .': ' . 'DbProject()<BR/>';
        
        parent::__construct();
        // If risico id get risico data
        if (!empty ($id)){
            $this->DbGetProjectData($id);
        }
    }

    //maak een nieuwe rij in de tabel
    public function save ($sjabloon_versie_id, $risicoaandachtsgebied_naam, $risicoaandachtsgebied_beschrijving){
        
        $query = "INSERT INTO `tpl_risico_aandachtsgebied` (`risico_aandachtsgebied_id`, `risico_aandachtsgebied_naam`, `risico_aandachtsgebied_beschrijving`) VALUES ('" . $this->risicoaandachtsgebied_id . "', '" . $risicoaandachtsgebied_naam . "', '" . $risicoaandachtsgebied_beschrijving . "')" or die ( "FOUTMELDING:  " . mysql_error());;
                
       if(!$this->dbquery($query)){
            
            echo(DB_TPL_ERROR_INSERT_RAG);
            return false;
        
        }
		$this->risicoaandachtsgebied_id = mysql_insert_id();
        
        $query2 = "INSERT INTO `tpl_risico_aandachtsgebied_versie` (`risico_aandachtsgebied_id`, `sjabloon_versie_id`) VALUES ('" . $this->risicoaandachtsgebied_id . "', '" . $sjabloon_versie_id . "')";
        
        if(!$this->dbquery($query2)){
            
            echo(DB_TPL_ERROR_INSERT_RAG);
            return false;
        
        }       
			
    }
  
	public function getRisicoaandachtsgebied_id(){
        
        return $this->risicoaandachtsgebied_id;
    }
	
    public function getProject_id(){            
        $rag = new DbRisicogebied($id);
        $id = $rag->getRisico_id();
        return $this->project_id;        
    }
        
    public function getRisicoaandachtsgebied(){
        
        return $this->risicoaandachtsgebied;
    }


    public function updateProject($nieuweactie){
	
		// check of actie_id bestaat of return false
		if(empty($this->Project_id)) return false;
		// update actie in database
						   
		$query = "UPDATE `project` SET project = '". $nieuweactie . "' WHERE project_id = '" . $this->project_id  . "'";
		mysql_query($query);
		$this->project = $nieuweactie;
		
		if (DEBUG_LVL & DEBUG_LVL_QUERY)  echo __FILE__ . ' ' . __LINE__ .': ' .$query . '<br />';
		return true;
		
	}
	public function deleteProject(){
	
		// check of actie_id bestaat of return false
		if(empty($this->Project_id)) return false;
		// update actie in database
		//UPDATE `cofely2`.`actie` SET `actie` = 'nieuwe actie' WHERE `actie`.`actie_id` =32 LIMIT 1 ;
						   
		$query = "DELETE FROM `project` WHERE project_id = '" . $this->project_id  . "'";
		mysql_query($query);
		
		$this->project_id = '';
		$this->risicoaandachtsgebied_id = '';
		$this->risicoaandachtsgebied = '';
		
		if (DEBUG_LVL & DEBUG_LVL_QUERY)  echo __FILE__ . ' ' . __LINE__ .': ' .$query . '<br />';
		return true;
		
	}
	
	public function getProjectArray(){
		return $this->array_project_id;
	}
    private function DbGetProjectData($id){
        
        $query = "SELECT project_id, risicoaandachtsgebied FROM project WHERE project_id = '" . $id . "' LIMIT 1 ";
		$result = mysql_query($query);
        $res = mysql_fetch_object($result);
        
        //$this->actieid = $res->actie_id;
        $this->risicoaandachtsgebied_id = $id;
        $this->project_id  = $res->project_id ;
        $this->risicoaandachtsgebied = $res->risicoaandachtsgebied;
		
        if (DEBUG_LVL & DEBUG_LVL_QUERY)  echo __FILE__ . ' ' . __LINE__ .': ' .$query . '<br />';
		
		
		//Haal de actie's uit de database die bij deze maatregel horen.
		//query maken
        $query = mysql_query("SELECT risicoaandachtsgebied_id, risicoaandachtsgebied FROM project WHERE project_id = '" . $id . "' LIMIT 1 ");
		while ($row = mysql_fetch_array($query) ){
		
			//$this->array_preventieve_maatregel_id[$row['preventieve_maatregel_id'] = $row['preventieve_maatregel']];
		}
    }
    
    public function __destruct() {
        parent::__destruct();
    }
}

class TestDbProject{
    public function TestDbProject(){
        $test = new DbProject(1);
        
        //getters
		echo $test->getRisicoaandachtsgebied_id() . " Risico aandachtsgebied id id<br />";
		echo $test->getProject_id() . " Project id<br />";
		echo $test->getRisicoaandachtsgebied() . " Risicoaandachtsgebied<br />";

        // save
        $test->save('3', 'test2');
		
        		echo $test->getProject_id() . " Project id<br />";
				
				
				$test2 = new DbProject(8);
				print_r($test2);
				echo $test2->getRisicoaandachtsgebied_id() . " Risicoaandachtsgebied id<br />";
				echo $test2->getProject_id() . " Project id<br />";
				echo $test2->getRisicoaandachtsgebied() . " Risicoaandachtsgebied<br />";
				
				if(!($test->updateProject(" nieuwe tekst "))) echo "Fout in updaten";
				print_r($test);
				
				if(!($test->deleteProject())) echo "Fout in delete";
				print_r($test);
        unset ($test);
		unset ($test2);
		
		
    }
}
?>